//
//  ConstraintsScrollView.swift
//  suda
//
//  Created by youai on 2019/7/19.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import UIKit
import SnapKit


///可用于自动布局的scrollView
class ConstraintsScrollView : UIScrollView{
    let contentView = UIView()
    init(){
        super.init(frame:CGRect.zero)
        self.addSubview(contentView)
        let testInset:CGFloat = 0
        contentView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(testInset)
            make.right.equalToSuperview().offset(-testInset)
            make.top.equalToSuperview().offset( testInset)
            make.bottom.equalToSuperview().offset(-testInset)
            make.width.equalToSuperview().offset(-testInset-testInset)
        }
    }
    
    ///向contentView添加视图
    func contentAddSubview(_ view:UIView){
        self.contentView.addSubview(view)
    }
    
    ///设定视图的底部为整个contentView的高度
    func bottomEqualTo(_ view:UIView,offset:CGFloat = 0){
        contentView.snp.makeConstraints { (make) in
            make.bottom.equalTo(view.snp.bottom).offset(offset).priority(.low)
            make.height.greaterThanOrEqualTo(self.snp.height)
        }
    }
    
    var hasContentHeight = false
    func setContentHeight(_ height:CGFloat){
        if !hasContentHeight {
            contentView.snp.makeConstraints { (make) in
                make.height.equalTo(height)
            }
            hasContentHeight = true
        }else{
            contentView.snp.updateConstraints { (make) in
                make.height.equalTo(height)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
